package com.mfin.bs.mappers.impl;

import com.mfin.bom.interfaces.ICashbook;
import com.mfin.bom.interfaces.ICustomer;
import com.mfin.bom.interfaces.IFinance;
import com.mfin.bom.interfaces.IFinanceLoan;
import com.mfin.bom.interfaces.IPartner;
import com.mfin.bom.interfaces.IUser;
import com.mfin.vo.CashbookVO;
import com.mfin.vo.CustomerVO;
import com.mfin.vo.PartnerVO;
import com.mfin.vo.UserVO;
import com.mfin.vo.finance.FinanceVO;
import com.mfin.vo.interfaces.IMappable;
import com.mfin.vo.loan.FinanceLoanVO;

public class CashbookMapper extends BaseMapper {

	@Override
	protected boolean doMap(IMappable fromArg, IMappable toArg) {
		boolean canPerformMap = false;

		if (fromArg instanceof ICashbook && toArg instanceof CashbookVO) {
			canPerformMap = true;
			performMap((ICashbook) fromArg, (CashbookVO) toArg);
		} else if (fromArg instanceof CashbookVO && toArg instanceof ICashbook) {
			performMap((CashbookVO) fromArg, (ICashbook) toArg);
			canPerformMap = true;
		}
		return canPerformMap;
	}

	private void performMap(CashbookVO fromArg, ICashbook toArg) {
		
		if (fromArg.getCashbookId() != null) {
			toArg.setCashbookId(fromArg.getCashbookId());
		}

		toArg.setDate(fromArg.getDate());
		toArg.setAccountCategory(fromArg.getAccountCategory());
		toArg.setAccountHead(fromArg.getAccountHead());
		toArg.setCredit(fromArg.getCredit());
		toArg.setDebit(fromArg.getDebit());
		toArg.setPersonName(fromArg.getPersonName());
		toArg.setBankName(fromArg.getBankName());
		toArg.setTransactionMode(fromArg.getTransactionMode());
		toArg.setChequeNumber(fromArg.getChequeNumber());
		toArg.setTransactionType(fromArg.getTransactionType());
		toArg.setPurpose(fromArg.getPurpose());
		toArg.setTowards(fromArg.getTowards());
		toArg.setRemarks(fromArg.getRemarks());
	}

	private void performMap(ICashbook fromArg, CashbookVO toArg) {
		
		if (fromArg.getCashbookId() != null) {
			toArg.setCashbookId(fromArg.getCashbookId());
		}

		if (fromArg.getUser() != null) {
			IUser bomUser = fromArg.getUser();
			UserVO userVO = new UserVO();
			map(bomUser, userVO, false, true);
			toArg.setUser(userVO);
		}

		if (fromArg.getCustomer() != null) {
			ICustomer bomCustomer = fromArg.getCustomer();
			CustomerVO customerVO = new CustomerVO();
			map(bomCustomer, customerVO, false, true);
			toArg.setCustomer(customerVO);
		}

		if (fromArg.getFinanceLoan() != null) {
			IFinanceLoan bomFinanceLoan = fromArg.getFinanceLoan();
			FinanceLoanVO financeLoanVO = new FinanceLoanVO();
			map(bomFinanceLoan, financeLoanVO, false, true);
			toArg.setFinanceLoan(financeLoanVO);
		}

		if (fromArg.getFinance() != null) {
			IFinance bomFinance = fromArg.getFinance();
			FinanceVO financeVO = new FinanceVO();
			map(bomFinance, financeVO, false, true);
			toArg.setFinance(financeVO);
		}

		if (fromArg.getPartner() != null) {
			IPartner bomPartner = fromArg.getPartner();
			PartnerVO partnerVO = new PartnerVO();
			map(bomPartner, partnerVO, false, true);
			toArg.setPartner(partnerVO);
		}

		toArg.setDate(fromArg.getDate());
		toArg.setAccountCategory(fromArg.getAccountCategory());
		toArg.setAccountHead(fromArg.getAccountHead());
		toArg.setCredit(fromArg.getCredit());
		toArg.setDebit(fromArg.getDebit());
		toArg.setPersonName(fromArg.getPersonName());
		toArg.setBankName(fromArg.getBankName());
		toArg.setTransactionMode(fromArg.getTransactionMode());
		toArg.setChequeNumber(fromArg.getChequeNumber());
		toArg.setTransactionType(fromArg.getTransactionType());
		toArg.setPurpose(fromArg.getPurpose());
		toArg.setTowards(fromArg.getTowards());
		toArg.setRemarks(fromArg.getRemarks());
	}
}
